using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Tool
{
    public class EventSound
    {
        private string m_name;
        private SoundModifier m_volumeEditor;
        private SoundModifier m_panEditor;
        private SoundModifier m_distEditor;
        private SoundModifier m_reverbEditor;
        private int m_timeOffset; //time offset in miliseconds
        private int m_pitchShift;

        private bool m_isUsingVar4Volume;
        private bool m_isUsingVar4Pan;

        private string m_varName4Volume;
        private string m_varName4Pan;
        private string m_varName4Distortion;
        private string m_varName4Reverb;

        public EventSound(string name,int windowWidth,int windowHeight,int windowLeft,int windowTop, Editor parentForm, bool makePoints)
        {
            m_name = name;
            m_volumeEditor = new SoundModifier(EDIT_TYPE.EDIT_VOLUME,windowLeft,windowTop,windowWidth,windowHeight,makePoints);
            m_panEditor = new SoundModifier(EDIT_TYPE.EDIT_PAN, windowLeft, windowTop, windowWidth, windowHeight, makePoints);
            m_distEditor = new SoundModifier(EDIT_TYPE.EDIT_DISTORTION, windowLeft, windowTop, windowWidth, windowHeight, makePoints);
            m_reverbEditor = new SoundModifier(EDIT_TYPE.EDIT_REVERB, windowLeft, windowTop, windowWidth, windowHeight, makePoints);
            m_timeOffset = 0;
            m_isUsingVar4Volume = true;
            m_isUsingVar4Pan = true;
            m_varName4Volume = ((GlobalVariable)parentForm.m_globalVarManager.GetGlobalVariables()[0]).m_name;
            m_varName4Pan = ((GlobalVariable)parentForm.m_globalVarManager.GetGlobalVariables()[0]).m_name;
            m_varName4Distortion = ((GlobalVariable)parentForm.m_globalVarManager.GetGlobalVariables()[0]).m_name;
            m_varName4Reverb = ((GlobalVariable)parentForm.m_globalVarManager.GetGlobalVariables()[0]).m_name;
            m_pitchShift = 0;
        }

        public SoundModifier GetVolumeModifier()
        {
            return m_volumeEditor;
        }

        public SoundModifier GetPanModifier()
        {
            return m_panEditor;
        }

        public SoundModifier GetDistModifier()
        {
            return m_distEditor;
        }

        public SoundModifier GetReverbModifier()
        {
            return m_reverbEditor;
        }

        public string GetName()
        {
            return m_name;
        }

        public int GetTimeOffset()
        {
            return m_timeOffset;
        }

        public void SetTimeOffset( int newOffset )
        {
            m_timeOffset = newOffset;
        }

        public int GetPitchShift()
        {
            return m_pitchShift;
        }

        public void SetPitchShift(int pitchShift)
        {
            m_pitchShift = pitchShift;
        }

        public bool IsUsingVar(EDIT_TYPE type)
        {
            switch( type )
            {
                case EDIT_TYPE.EDIT_VOLUME:
                    {
                        return m_isUsingVar4Volume;
                    }
                case EDIT_TYPE.EDIT_PAN:
                    {
                        return m_isUsingVar4Pan;
                    }
                case EDIT_TYPE.EDIT_REVERB:
                    {
                        return true;
                    }
                case EDIT_TYPE.EDIT_DISTORTION:
                    {
                        return true;
                    }
            }
            return false;
        }

        public void SetIsUsingVar(EDIT_TYPE type, bool isUsingVar)
        {
            switch( type )
            {
                case EDIT_TYPE.EDIT_VOLUME:
                    {
                        m_isUsingVar4Volume = isUsingVar;
                        break;
                    }
                case EDIT_TYPE.EDIT_PAN:
                    {
                        m_isUsingVar4Pan = isUsingVar;
                        break;
                    }
            }
        }

        public string GetVarName(EDIT_TYPE type)
        {
            switch( type )
            {
                case EDIT_TYPE.EDIT_VOLUME:
                    {
                        return m_varName4Volume;
                    }
                case EDIT_TYPE.EDIT_PAN:
                    {
                        return m_varName4Pan;
                    }
                case EDIT_TYPE.EDIT_DISTORTION:
                    {
                        return m_varName4Distortion;
                    }
                case EDIT_TYPE.EDIT_REVERB:
                    {
                        return m_varName4Reverb;
                    }
            }
            return "";
        }

        public void SetVarName(EDIT_TYPE type, string name)
        {
            switch (type)
            {
                case EDIT_TYPE.EDIT_VOLUME:
                    {
                        m_varName4Volume = name;
                        break;
                    }
                case EDIT_TYPE.EDIT_PAN:
                    {
                        m_varName4Pan = name;
                        break;
                    }
                case EDIT_TYPE.EDIT_DISTORTION:
                    {
                        m_varName4Distortion = name;
                        break;
                    }
                case EDIT_TYPE.EDIT_REVERB:
                    {
                        m_varName4Reverb = name;
                        break;
                    }
            }
        }
    }

    public class Event
    {
        private string m_name;
        private ArrayList m_soundList = new ArrayList();
        bool m_loop;

        public Event(string name)
        {
            m_name = name;
            m_loop = false;
        }

        public string GetName()
        {
            return m_name;
        }

        public void AddSound(EventSound sound)
        {
            m_soundList.Add(sound);
        }

        public ArrayList GetSoundList()
        {
            return m_soundList;
        }

        public EventSound GetEventSound(int index)
        {
            return ((EventSound)m_soundList[index]);
        }

        public bool GetLoop()
        {
            return m_loop;
        }

        public void SetLoop(bool loop)
        {
            m_loop = loop;
        }
    }

    public class EventTree
    {
        public const string DEFAULT_VAR_NAME = "DefaultVariable";

        private System.Windows.Forms.Form m_parent;
        private ArrayList m_events = new ArrayList();

        public EventTree(System.Windows.Forms.Form parent)
        {
            m_parent = parent;
        }

        public ArrayList GetEventSounds(string eventName)
        {
            for (int i = 0; i < m_events.Count; ++i)
            {
                if (eventName == ((Event)m_events[i]).GetName())
                {
                    return ((Event)m_events[i]).GetSoundList();
                }
            }

            return new ArrayList();
        }

        public bool AddEvent(string name)
        {
            for (int i = 0; i < m_events.Count; ++i)
            {
                if (name == ((Event)m_events[i]).GetName())
                {
                    return false;
                }
            }
            m_events.Add(new Event(name));
            return true;
        }

        public void RemoveEvent(string name)
        {
            for (int i = 0; i < m_events.Count; ++i)
            {
                if (name == ((Event)m_events[i]).GetName())
                {
                    m_events.Remove(m_events[i]);
                }
            }
            ((Editor)m_parent).EventTimePanel.Invalidate();
        }

        public Event GetEvent(string name)
        {
            for (int i = 0; i < m_events.Count; ++i)
            {
                if (name == ((Event)m_events[i]).GetName())
                {
                    return ((Event)m_events[i]);
                }
            }
            return new Event("NoEvent");
        }

        public void AddSound(string eventName,string soundName)
        {
            if (((Editor)m_parent).m_globalVarManager.GetGlobalVariables().Count <= 0)
                ((Editor)m_parent).m_globalVarManager.AddGlobalVariable(DEFAULT_VAR_NAME);

            for (int i = 0; i < m_events.Count; ++i)
            {
                if (eventName == ((Event)m_events[i]).GetName())
                {
                    ((Event)m_events[i]).AddSound(new EventSound(soundName,
                                                                 ((Editor)m_parent).SoundEditorPanel.Width,
                                                                 ((Editor)m_parent).SoundEditorPanel.Height,
                                                                 ((Editor)m_parent).SoundEditorPanel.Left,
                                                                 ((Editor)m_parent).SoundEditorPanel.Top,
                                                                 (Editor)m_parent,true));
                }
            }

            ((Editor)m_parent).EventTimePanel.Invalidate();
        }

        public EventSound GetEventSound(string eventName, int soundIndex)
        {
            for (int i = 0; i < m_events.Count; ++i)
            {
                if (eventName == ((Event)m_events[i]).GetName())
                {
                    return ((Event)m_events[i]).GetEventSound(soundIndex);
                }
            }
            return null;
        }

        public void RemoveSound(string eventName, int soundIndex)
        {
            for (int i = 0; i < m_events.Count; ++i)
            {
                if (eventName == ((Event)m_events[i]).GetName())
                {
                    ArrayList soundList = ((Event)m_events[i]).GetSoundList();
                    soundList.Remove(soundList[soundIndex]);

                    ((Editor)m_parent).EventTimePanel.Invalidate();
                    return;
                }
            }
        }

        public ArrayList GetEvents()
        {
            return m_events;
        }
    }
}
